package com.sj.purchase.modules.cost.web;

import com.sj.purchase.common.Result;
import com.sj.purchase.common.ResultGenerator;
import com.sj.purchase.modules.cost.domain.WarehouseEnum;
import com.sj.purchase.modules.cost.repository.stock.SkuStockRepository;
import com.sj.purchase.modules.cost.service.stock.SkuStockReportService;
import com.sj.purchase.modules.cost.views.stock.SkuStockReportVO;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

import static com.sj.purchase.common.ResultGenerator.ok;

/**
 * sku库存报表 进销库存报表查询接口
 * Created by yangrd on 2017/9/14
 **/
@RestController
@RequestMapping("api/sku-stock-report")
public class SkuStockReportController {

    @Autowired
    private SkuStockRepository skuStockRepository;

    @Autowired
    private SkuStockReportService skuStockReportService;

    @GetMapping
    public Result<Page<SkuStockReportVO>> searchSkuStock(WarehouseEnum warehouseEnum, Date start, Date end, Pageable pageable) {
        Page<String> skuPage = skuStockRepository.findBaseStockSku(start, end, warehouseEnum.name(), pageable);
        List<String> skuNoList = skuPage.getContent();
        return ResultGenerator.ok(new PageImpl<>(skuStockReportService.search(warehouseEnum, start, end, skuNoList), pageable, skuPage.getTotalElements()));
    }

    @GetMapping("excel")
    public void exportExcel(WarehouseEnum warehouseEnum, Date start, Date end) {
        List<String> skuNoList = skuStockRepository.findBaseStockSku(start, end, warehouseEnum.name(), null).getContent();
        String dateSegment = SkuStockReportVO.getDateSegment(start, end);
        ExcelDownloadUtil.easyPoi(dateSegment + "进销库存报表", SkuStockReportVO.class, skuStockReportService.search(warehouseEnum, start, end, skuNoList));
    }


}
